package com.hjh.thymeleafdemo1.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hjh.thymeleafdemo1.entity.User;
import com.hjh.thymeleafdemo1.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserRepository userRepository;

    @Override
    public User login(String name, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        queryWrapper.eq("password",password);
        return userRepository.selectOne(queryWrapper);
    }

    @Transactional
    @Override
    public boolean insertUser(User user) {
        boolean flag = false;
        if(userRepository.insert(user) > 0){
            flag = true;
        }
        return flag;
    }

    @Transactional
    @Override
    public boolean deleteUser(Long id) {
        boolean flag = false;
        if(userRepository.deleteById(id) > 0){
            flag = true;
        }
        return flag;
    }

    @Transactional
    @Override
    public int updateUser(User user) {
        return userRepository.updateById(user);
    }

    @Override
    public User getUser(Long id) {
        return userRepository.selectById(id);
    }

    @Override
    public List<User> findAllUsers() {
        return userRepository.selectList(null);
    }

    @Override
    public User getUserByName(String name) {
        return userRepository.getUserByName(name);
    }

//    @Override
//    public List<User> queryUserList() {
//        return userRepository.selectList(null);
//    }
}
